extends TextureRect

@export var hover_color: Color = Color.GAINSBORO
@export var click_color: Color = Color.GRAY

signal button_down
signal button_up

var _anim: Tween
var _isHover: bool
var _isDown: bool

func _ready():
	mouse_entered.connect(_mouse_entered)
	mouse_exited.connect(_mouse_exited)


func _gui_input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				_isDown = true
				self_modulate = click_color
				button_down.emit()
			else:
				_isDown = false
				self_modulate = hover_color if _isHover else Color.WHITE
				button_up.emit()


func _mouse_entered():
	_isHover = true
	if _anim:
		_anim.kill()

	_anim = create_tween()
	_anim.tween_property(self, "self_modulate", hover_color, 0.1)


func _mouse_exited():
	_isHover = false
	
	if _isDown:
		return

	if _anim:
		_anim.kill()
	
	_anim = create_tween()
	_anim.tween_property(self, "self_modulate", Color.WHITE, 0.1)
